#@Project HyperNetwork/MatrixHyperNetwork
#@CreatTime 2022/4/22
#@Author 李殊哲
#@E-mail ShuZheLi@126.com
#@Descripes 通过邻接矩阵和关联矩阵实现的超图网络

class MatrixHyperNetwork:
    def __init__(self,n):
        self.adjacencyMatrix=[[None]*n for i in range(n)]
        for i in range(n):
            self.adjacencyMatrix[n][n]=True
        self.incidenceMatrix = []
        self.NodeNum=n
        self.EageNum=0

    def Connect(self,nodes):
        if self.IsConnect(nodes):
            return False
        self.connectInIncidence(nodes)
        self.connectInAdjacence(nodes)
        self.EageNum+=1
        return True

    def connectInAdjacence(self, nodes):
        for i in nodes:
            for j in nodes:
                self.adjacencyMatrix[i][j] = True

    def connectInIncidence(self, nodes):
        self.incidenceMatrix.append([False * self.NodeNum])
        for n in nodes:
            self.incidenceMatrix[len(self.incidenceMatrix) - 1][n] = True

    def IsConnect(self,nodes):
        return self.__findEdge(nodes) is not None

    def __findEdge(self,nodes):
        for e in self.incidenceMatrix:
            flag = True
            for n in nodes:
                if e[n]==False:
                    flag=False
                    break
            if flag:
                return e
        return None

    def Disconnect(self,nodes):
        e=self.__findEdge(nodes)
        if e is None:
            return False
        else:
            self.adjacencyMatrix.remove(e)
            for n in e:
                for i in range(n):
                    self.adjacencyMatrix[n][i]=False
                self.__resetNodeAdjacence(n)
            return True

    def __resetNodeAdjacence(self, n):
        for i in range(n):
            if self.incidenceMatrix[i][n]:
                for j in range(n):
                    if self.incidenceMatrix[i][j]:
                        self.adjacencyMatrix[n][j] = True



